<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>老师注册</title>
    <link rel="stylesheet" type="text/css" href="CSS/bootstrap.css">
    <script type="text/javascript" src="JS/bootstrap.min.js"></script>
    <script type="text/javascript" src="JS/jquery.js"></script>
    <style>
        body{
            background: url(img/5.jpg);
            font-family: "microsoft yahei";
            min-width: 800px;
        }
        img{
            width: 100%;
            height:auto;
        }
    </style>
</head>
<body>
<div>
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <button class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-title">
                <h1 class="text-center">老师注册</h1>
            </div>
            <div class="modal-body">
                <form class="form-group" action="" method="post">
                    <div class="form-group">
                        <label for="">昵称</label>
                        <input class="form-control" type="text" placeholder="不超过五个字" name="tea_name" id="tea_name">    
                    </div>
                    <div class="form-group">
                        <label for="">账号</label>
                        <input class="form-control" type="text" placeholder="6-15位字母或数字" name="tea_number">
                    </div>
                    <div class="form-group">
                        <label for="">密码</label>
                        <input class="form-control" type="password" placeholder="至少6位字母或数字" name="passwd">
                    </div>
                    <div class="form-group">
                        <label for="">确认密码</label>
                        <input class="form-control" type="password" placeholder="至少6位字母或数字" name="again_passwd">
                    </div>
                    <div class="text-right">
                        <input class="btn btn-primary" type="submit" name="submit" value="提交">
                        <input class="btn btn-danger" data-dismiss="modal" type="submit" name="quxiao" value="取消">
                    </div>
                    <a href="index.php" data-toggle="modal" data-dismiss="modal">已有账号？点我登录</a>
                </form>
            </div>
        </div>
    </div>
</div>

<?php
if(isset($_POST["submit"]) && $_POST["submit"] == "提交") {

    date_default_timezone_set("Asia/Shanghai");
    $insertdate = date("Y-m-d H:m:s");
    $tea_number = $_POST["tea_number"];
    $passwd = md5($_POST["passwd"]);
    $again_passwd = md5($_POST["again_passwd"]);
    $tea_name = $_POST["tea_name"];

    $selectteasql = "select * from teachers where tea_name='$tea_name'";

    include_once("database.php");

    $result = @mysql_query($selectteasql) or die("查询同名用户的时候出现错误!");
    
    $result = mysql_num_rows($result);

    if($result > 0) {
        //存在同名记录
        echo "<font color='red'>昵称<span style='color:blue;font-size: 20px'>{$tea_name}</span>已经被注册,请重新填写用户名!</font>";
    } else {

        if($passwd == $again_passwd) {

            $insertteasql = "insert into teachers(tea_number,tea_password,tea_name,tea_insertdate) 
            values('$tea_number','$passwd','$tea_name','$insertdate')";
            
            include_once("database.php");

            @mysql_query($insertteasql) or die("用户注册失败:".mysql_error()."<br/>");

            header("Location:index.php");

        } else {
            echo "<script>alert('两次密码不一致！');</script>";
        }
    }
}

if(isset($_POST["quxiao"]) && $_POST["quxiao"] == "取消") {
    header("Location:index.php");
}
?>

</body>
</html>